package com.nebula.salary.portal.service.impl;

import com.nebula.salary.common.constants.Const;
import com.nebula.salary.common.constants.ReturnMessage;
import com.nebula.salary.common.exception.ErrorException;
import com.nebula.salary.common.utils.SecurityUtil;
import com.nebula.salary.model.pojo.Question;
import com.nebula.salary.portal.mapper.QuestionMapper;
import com.nebula.salary.portal.service.IQuestionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Strings;

import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 密保模板表 服务实现类
 * </p>
 *
 * @author codex
 * @since 2022-01-24
 */
@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements IQuestionService {

	@Override
	public void checkData(Question question) {
		
		if (question == null) {
			throw new ErrorException(ReturnMessage.ILLEGAL_REQUEST);
		}
		
		if (Strings.isNullOrEmpty(question.getQuestion())) {
			throw new ErrorException("密保问题不能为空");
		}
	}

	@Override
	public String checkQuestionUnique(Question question) {

		int qId = question.getQuestionId() == null ? -1 : question.getQuestionId();

		question.setQuestion(question.getQuestion().replace("?", "？"));
		
		Question q = baseMapper.selectOne(new QueryWrapper<Question>().eq(Question.QUESTION, question.getQuestion()));
		
		if (q != null && q.getQuestionId() != qId) {
			return Const.NOT_UNIQUE;
		}
		
		return Const.UNIQUE;
	}
	
	/**
	 * 获取当前用户的密保问题
	 *
	 * @param userId 用户ID
	 * @return 密保问题
	 */
    @Override
    public List<Question> questionUserList(Long userId) {
		return baseMapper.getQuestionUserList(userId);
    }
}
